/*
 * Copyright (C) 2021 Huawei Device Co., Ltd.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package io.sentry.config;

import io.sentry.ILogger;
import io.sentry.SystemOutLogger;

import java.util.ArrayList;
import java.util.List;
import java.util.Properties;

import org.jetbrains.annotations.ApiStatus;
import org.jetbrains.annotations.NotNull;

/**
 * PropertiesProviderFactory
 *
 * @since 2021-06-02
 */

@ApiStatus.Internal
public final class PropertiesProviderFactory {
    /**
     * Creates a composite properties provider enabling properties resolution from following locations
     * in order:
     *
     * <ul>
     *   <li>system properties
     *   <li>environment variables
     *   <li><code>sentry.properties</code> file which location is resolved from the system property
     *       <code>sentry.properties.file</code>
     *   <li><code>sentry.properties</code> file which location is resolved from the environment
     *       variable <code>SENTRY_PROPERTIES_FILE</code>
     *   <li><code>sentry.properties</code> located in the root of the classpath
     *   <li><code>sentry.properties</code> located in the application's current working directory
     * </ul>
     *
     * @return the properties provider
     */
    public static @NotNull PropertiesProvider create() {
        final ILogger logger = new SystemOutLogger();
        final List<PropertiesProvider> providers = new ArrayList<>();
        providers.add(new SystemPropertyPropertiesProvider());
        providers.add(new EnvironmentVariablePropertiesProvider());

        final String systemPropertyLocation = System.getProperty("sentry.properties.file");
        if (systemPropertyLocation != null) {
            final Properties properties =
                new FilesystemPropertiesLoader(systemPropertyLocation, logger).load();
            if (properties != null) {
                providers.add(new SimplePropertiesProvider(properties));
            }
        }

        final String environmentVariablesLocation = System.getenv("SENTRY_PROPERTIES_FILE");
        if (environmentVariablesLocation != null) {
            final Properties properties =
                new FilesystemPropertiesLoader(environmentVariablesLocation, logger).load();
            if (properties != null) {
                providers.add(new SimplePropertiesProvider(properties));
            }
        }

        final Properties properties = new ClasspathPropertiesLoader(logger).load();
        if (properties != null) {
            providers.add(new SimplePropertiesProvider(properties));
        }

        final Properties runDirectoryProperties =
            new FilesystemPropertiesLoader("sentry.properties", logger).load();
        if (runDirectoryProperties != null) {
            providers.add(new SimplePropertiesProvider(runDirectoryProperties));
        }

        return new CompositePropertiesProvider(providers);
    }
}
